package com.admin.action

import com.admin.service.AdminIndexService
import com.data.annotation.NoargAnnotation
import com.data.annotation.OpenAnnotation
import com.data.bean.Account
import com.data.util.FinalStringUtils
import com.data.util.ImageLookUtils
import com.data.util.JsonUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.commons.CommonsMultipartFile
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

@Controller
@OpenAnnotation
@NoargAnnotation
@RequestMapping("/adminIndex/admin/")
class AdminIndexAction {
    @Autowired
    private var adminIndexService: AdminIndexService? = null


    /**
     * 进入到首页
     *
     * @param request
     * @return
     */
    @GetMapping("index")
    fun indexAdminAction(request: HttpServletRequest): String {
        val account = request.session.getAttribute(FinalStringUtils.ACCOUNT) as Account
        return if (account.types == FinalStringUtils.ADMIN) {
            "WEB-INF/admin/index"
        } else "redirect:/login"
    }

    /**
     * 进入到首页
     *
     * @param request
     * @return
     */
    @GetMapping("home")
    fun indexHomeAction(request: HttpServletRequest): String {
        //TODO://处理,统计信息，提示消息等
        return "WEB-INF/admin/home"

    }

    /**
     * 首页展示图信息展示
     *
     * @param request
     * @return
     */
    @GetMapping("list")
    fun indexBannerListAction(request: HttpServletRequest): String {
        val list = adminIndexService!!.indexBannerListService()
        request.setAttribute("dataMap", list)
        return "WEB-INF/admin/index/indexList"
    }

    /**
     * 查看图片
     *
     * @param image
     * @param response
     */
    @GetMapping("image")
    fun showImageAction(image: String, response: HttpServletResponse) {
        val inputStream = adminIndexService!!.showImageService(image)
        //响应图片
        if (inputStream != null)
            ImageLookUtils.responseImage(inputStream, response)

    }

    /**
     * 删除展示图信息展示
     *
     * @param request
     * @return
     */
    @PostMapping("delete")
    @ResponseBody
    fun deleteBannerListAction(id: String, request: HttpServletRequest): Map<String, Any> {
        val bool = adminIndexService!!.deleteBannerListService(id)
        return JsonUtils.returnMassageUtils(bool, if (bool) "删除成功" else "删除失败")
    }

    /**
     * 修改这个首页的展示图的状态，是否启用，或者禁用
     *
     * @param id
     * @param state
     * @param request
     * @return
     */
    @PostMapping("changeState")
    @ResponseBody
    fun changeStateAction(id: String, state: Boolean, request: HttpServletRequest): Map<String, Any> {

        val bool = adminIndexService!!.changeStateService(id, state)
        return JsonUtils.returnMassageUtils(bool, if (bool) "操作成功" else "操作失败")
    }

    /**
     * 进入到添加banner图的界面
     *
     * @param request
     * @return
     */
    @RequestMapping("addIndex")
    fun addIndexBannerAction(request: HttpServletRequest): String {

        return "WEB-INF/admin/index/addIndex"
    }

    /**
     * 添加banner的操作
     *
     * @param image   展示图
     * @param bImage  背景图
     * @param request
     * @return
     */
    @PostMapping("submitIndex")
    @ResponseBody
    fun submitIndexBannerAction(@RequestParam("image") image: CommonsMultipartFile, @RequestParam("bImage") bImage: CommonsMultipartFile, title: String, type: Int, request: HttpServletRequest): Map<String, Any> {
        val bool = adminIndexService!!.submitIndexBannerService(image, bImage, title, type)
        return JsonUtils.returnMassageUtils(bool, if (bool) "添加成功" else "添加失败")
    }
}
